<template>
  <a-modal
    v-model:visible="visible"
    draggable
    width="680px"
    @before-ok="handleBeforeOk"
    @cancel="handleChangeVisible"
    @close="close"
  >
    <template #title> {{ title }}</template>
    <a-form
      ref="formRef"
      :model="formData"
      class="form"
      :label-col-props="{ span: 6 }"
      :wrapper-col-props="{ span: 18 }"
    >
      <a-form-item
        field="remark"
        label="废除删除备注"
        :rules="[{ required: true, message: '请输入废除删除备注' }]"
      >
        <a-input v-model="formData.remark" placeholder="请输入废除删除备注" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { Message } from '@arco-design/web-vue';
  import { delProcessInstanceByFormId } from '@/api/agency';

  defineProps({
    title: {
      type: String,
      default: '备注',
    },
    portalData: {
      type: Array,
    },
    renderData: {
      type: Array,
    },
  });

  const formRef = ref();
  const visible = ref(false);
  const positionForm = () => {
    return {
      remark: '',
      formId: '',
    };
  };
  const formData = ref(positionForm());
  const isEdit = ref(false);
  const handleChangeVisible = (formId: string) => {
    visible.value = !visible.value;
    formData.value.formId = formId;
    visible.value = true;
  };
  const close = () => {
    visible.value = false;
    formData.value = positionForm();
    formRef.value.clearValidate();
  };

  const emit = defineEmits(['refresh']);

  function handleBeforeOk(done: any) {
    formRef.value.validate((valid: any) => {
      if (valid) {
        done(false);
      } else {
        delProcessInstanceByFormId(formData.value.formId, formData.value.remark)
          .then(() => {
            Message.success('操作成功');
            emit('refresh');
            done();
          })
          .catch(() => {
            done(false);
          });
      }
    });
  }

  defineExpose({ handleChangeVisible });
</script>
